package com.test.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.comment.FightBean;
import com.test.comment.RaceBean;

import com.test.comment.SingerRaceBean;
import com.test.comment.UserBean;
import com.test.dao.UserDao;
import com.test.services.ActiveAdminServices;

@Controller
@ResponseBody
@RequestMapping("/race")
public class RaceController {
	private Logger logger = Logger.getLogger(getClass());
	@Autowired
	public UserDao userDao;
	@Autowired
	ActiveAdminServices activeAdminServices;
	@RequestMapping("/active")
	public ModelAndView goactive() {
		logger.debug("11111");
		return new ModelAndView("WEB-INF/jsp/ActiveAdmin");
	}
	@RequestMapping("/userss")
	public Map<String, Object> userss(HttpSession session){
		
		
		UserBean userBean = (UserBean)session.getAttribute("user");
		Map<String, Object> map = new HashMap<>();
		map.put("user",userBean);
		return map;
	}
	@RequestMapping("/allrace")
	public Map<String, Object> allrace(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit){
		int tmp = (page-1)*limit;
		int tol = userDao.totalrace();
		logger.debug("========总人数:"+tol);
		List<RaceBean> raceBeans = userDao.racelimit(tmp, limit);
		
		Map<String, Object> map = new HashMap<>();
		 map.put("tol", tol);	
		map.put("raceBeans", raceBeans);
		map.put("message", "查询成功sss");
	    map.put("status", 200);
	    return map;
		
	}
	@RequestMapping("/allsinger")
	public Map<String, Object> allsinger(@RequestParam("page")Integer page,
			@RequestParam("limit")Integer limit,@RequestParam("rid")Integer rid){
		int tmp = (page-1)*limit;
		logger.debug("========rid:"+rid);
		int tol = userDao.totalsingerrace(rid);
		logger.debug("==========总数:"+tol);
		List<SingerRaceBean> singerRaceBeans = activeAdminServices.singerrace(tmp, limit, rid);
		
		
		Map<String, Object> map = new HashMap<>();
		 map.put("tol", tol);	
		map.put("singerRaceBeans", singerRaceBeans);
		map.put("message", "查询成功sss");
	    map.put("status", 200);
	    return map;
	}
	@RequestMapping("/insertrace")
	public Map<String, String> insertrace(@RequestParam("raceid")Integer raceid,
			@RequestParam("singerid1")Integer singerid1,@RequestParam("singerid2")Integer singerid2){
		logger.debug("=====raceid:"+raceid);
		logger.debug("===========singerid1:"+singerid1);
		logger.debug("==========singerid2:"+singerid2);
		activeAdminServices.insertfight(raceid, singerid1, singerid2);
		
		Map<String, String> map = new HashMap<>();
		return map;
	}
	@RequestMapping("/updatarace")
	public Map<String, String> updatarace(@RequestParam("rid")Integer rid,@RequestParam("field")String field,
			@RequestParam("value")String value){
		logger.debug("============rid:"+rid);
		logger.debug("============field:"+field);
		logger.debug("============value:"+value);
		RaceBean raceBean = new RaceBean();
		raceBean.setRid(rid);
		if(field.equals("raceName")) {
			raceBean.setRaceName(value);
		}
		if(field.equals("raceAddress")) {
			raceBean.setRaceName(value);
		}
		if(field.equals("raceTime")) {
			raceBean.setRaceName(value);
		}
		if(field.equals("include")) {
			raceBean.setRaceName(value);
		}
		userDao.updaterace(raceBean);
		logger.debug("========修改后的比赛名:"+raceBean.getRaceName());
		Map<String, String> map = new HashMap<>();
		return map;
	}
	@RequestMapping("/allra")
	public Map<String, Object> allra(){
		List<RaceBean> raceBeans = userDao.allra();
		
		Map<String, Object> map = new HashMap<>();
		 map.put("tol", 0);	
		map.put("raceBeans", raceBeans);
		map.put("message", "查询成功sss");
	    map.put("status", 200);
	    return map;
	}
	@RequestMapping("/racesinger")
	public Map<String, Object> racesinger(@RequestParam("page")Integer page,
			@RequestParam("limit")Integer limit,@RequestParam("rid")Integer rid,Integer tmps){
		logger.debug("=========rid:"+rid);
		
		int tmp = (page-1)*limit;
		int tol = userDao.totalracesinger(rid);
		List<FightBean> fightBeans = activeAdminServices.racesinger(tmp,limit,rid);
		Map<String, Object> map = new HashMap<>();
		 map.put("tol", tol);	
		map.put("fightBeans", fightBeans);
		map.put("message", "查询成功sss");
	    map.put("status", 200);
	    return map;
		
	}

	@RequestMapping("/insertraceinfor")
	public Map<String, String> insertrace(RaceBean raceBean){
		logger.debug("===============插入:"+raceBean.getRaceName());
		logger.debug("===============插入:"+raceBean.getInclude());
		logger.debug("===============插入:"+raceBean.getRaceTime());
		logger.debug("===============插入:"+raceBean.getRaceAddress());
		userDao.insertraceinfor(raceBean);
		Map<String, String> map = new HashMap<>();
		map.put("msg", "添加成功");
		return map;
	}
	@RequestMapping("/deleterace")
	public Map<String, String> deleterace(Integer rid){
		logger.debug("===========删除"+rid);
		userDao.deleterace(rid);
		Map<String, String> map = new HashMap<>();
		map.put("msg", "添加成功");
		return map;
	}
	@RequestMapping("/startfight")
	public Map<String, String> startfight(Integer fid,Integer rid){
		logger.debug("=========fid:"+fid);
		logger.debug("============rid:"+rid);
		String string = activeAdminServices.updatefight(fid,rid);
		logger.debug("=============="+string);
		Map<String, String> map = new HashMap<>();
		map.put("msg", string);
		return map;
	}
	@RequestMapping("/stopfight")
	public FightBean stopfight(Integer fid,Integer rid){
		logger.debug("=========fid:"+fid);
		logger.debug("============rid:"+rid);
		FightBean fightBean = activeAdminServices.stopfight(fid,rid);
		
		return fightBean;
	}
	@RequestMapping("/singerscore")
	public Map<String, Object> singerscore(Integer page,Integer limit,Integer rid){
		logger.debug("==============page:"+page);
		logger.debug("==============limit:"+limit);
		logger.debug("==============rid:"+rid);
		int tmp = (page-1)*limit;
		int tol = userDao.findsingerscore(rid);
		List<SingerRaceBean> singerRaceBeans = userDao.singerscore(tmp,limit,rid);
		Map<String, Object> map = new HashMap<>();
		 map.put("tol", tol);	
		map.put("singerRaceBeans", singerRaceBeans);
		map.put("message", "查询成功sss");
	    map.put("status", 200);
	    return map;
		
	}
	@RequestMapping("/deletefights")
	public Map<String, String> deletefights(Integer fid){
		userDao.deletefights(fid);
		Map<String, String> map = new HashMap<>();
		return map;
	}

}
